<?php

class Core_Model_DbTable_EntryFields extends YouNet_Db_Table implements Core_Model_IEntryFields
{
	const TABLE_NAME			= 'entry_fields';
    protected $_name            = self::TABLE_NAME;
    protected $_rowClass        = 'Core_Model_EntryField';

	protected $_referenceMap = array(
		'Contest' => array(
			'columns' => 'id_contest',
			'refTableClass' => 'Core_Model_DbTable_Contests',
			'refColumns' => 'id'
		),
	);

	public function getListFieldsBelongToContest($id_contest)
	{
		return $this->_getListFieldsByContestId($id_contest, 0);
	}

	public function getListFieldsBelongToEntry($id_contest)
	{
		return $this->_getListFieldsByContestId($id_contest, 1);
	}

	/**
	 *
	 * @param type $id_contest
	 * @param type $is_entry determine this field is apply for each new entry or
	 * every entry (e.g: each entry has a different recipe)
	 * @return type
	 */
	private function _getListFieldsByContestId($id_contest, $is_entry = 0)
	{
		$query = $this->select()
					->where('id_contest=?',$id_contest)
					->where('is_entry  =?',$is_entry)
                    ->where('is_displayed = ? OR type = ' . self::INDEX_FULL_NAME, Core_Model_IEntryFields::STATUS_DISPLAYED)                    
					->order('type asc');

		return $this->fetchAll($query);
	}

    /**
     * 
     * @param integer $id_contest
     * @param integer $type
     * @return Core_Model_EntryField
     */
    public function getFieldByIdContest($id_contest, $type)
	{
		$query = $this->select()
					->where('id_contest= ?', $id_contest)
					->where('is_entry  = ?', 0)
                    ->where('type = ?', $type);
		return $this->fetchRow($query);
	}
    
	public static function getListFieldTypes()
	{
		static $arrFieldType = array();
		if( empty($arrFieldType) )
		{
			$arrFieldType[self::INDEX_FULL_NAME] = array(
				'label'				=> self::FIELD_FULL_NAME,
                'class'             => 'length_longest',
				'is_system'			=> 1,
				'type'				=> self::TYPE_TEXT,
				'is_required'		=> true,
				'validator'			=> array(
					array('StringLength',array('max'=>255))
				),
				'filter'			=> array('StringTrim')
			);
			$arrFieldType[self::INDEX_FIRST_NAME] = array(
				'label'				=> self::FIELD_FIRST_NAME,
                'class'             => 'length_longest',
				'is_system'			=> 1,
				'type'				=> self::TYPE_TEXT,
				'is_required'		=> true,
				'validator'			=> array(
					array('StringLength',array('max'=>255))
				),
				'filter'			=> array('StringTrim')
			);
			$arrFieldType[self::INDEX_MIDDLE_NAME] = array(
				'label'				=> self::FIELD_MIDDLE_NAME,
                'class'             => 'length_longest',
				'is_system'			=> 1,
				'type'				=> self::TYPE_TEXT,
				'is_required'		=> false,
				'validator'			=> array(
					array('StringLength',array('max'=>255))
				),
				'filter'			=> array('StringTrim')
			);
			$arrFieldType[self::INDEX_LAST_NAME] = array(
				'label'				=> self::FIELD_LAST_NAME,
                'class'             => 'length_longest',
				'is_system'			=> 1,
				'type'				=> self::TYPE_TEXT,
				'is_required'		=> true,
				'validator'			=> array(
					array('StringLength',array('max'=>255))
				),
				'filter'			=> array('StringTrim')
			);
			$arrFieldType[self::INDEX_EMAIL] = array(
				'label'				=> self::FIELD_EMAIL,
                'class'             => 'length_longest',
				'is_system'			=> 0,
				'type'				=> self::TYPE_TEXT,
				'is_required'		=> true,
				'validator'			=> array(
					array('EmailAddress'),
					array('StringLength', array('max'=>100))
				),
				'filter'			=> array('StringTrim')
			);
			$arrFieldType[self::INDEX_MAILLING_ADDRESS] = array(
				'label'				=> self::FIELD_MAILLING_ADDRESS,
                'class'             => 'length_longest',
				'is_system'			=> 0,
				'is_required'		=> false,
				'type'				=> self::TYPE_TEXT,
				'validator'			=> array(
					array('StringLength',array('max'=>255))
				),
				'filter'			=> array('StringTrim')
			);
			/***
			 * Regex: input value can only contain + .-()[0-9]
			 * and end with a number
			 */
			$arrFieldType[self::INDEX_PHONE_NUMBER] = array(
				'label'				=> self::FIELD_PHONE_NUMBER,
                'class'             => 'length_medium',
				'is_system'			=> 0,
				'type'				=> self::TYPE_TEXT,
				'is_required'		=> false,
				'regex_error'		=> 'ERROR_INVALID_PHONE_NUMBER',
				'validator'			=> array(
					array('Regex', true, array('/^[\d\+\s()-.]*[\d]$/'))
				),
				'filter'			=> array('StringTrim')
			);
			$arrFieldType[self::INDEX_DATE_OF_BIRTH] = array(
				'label'				=> self::FIELD_DATE_OF_BIRTH,
                'class'             => 'length_medium',
				'is_system'			=> 0,
				'type'				=> self::TYPE_TEXT,
				'is_required'		=> false,
				'validator'			=> array(
					array('Date', false, array(BIRTHDAY_FORMAT))
				),
				'filter'			=> array('StringTrim'),
			);
			$arrFieldType[self::INDEX_IDENTIFICATION_NUMBER] = array(
				'label'				=> self::FIELD_IDENTIFICATION_NUMBER,
                'class'             => 'length_medium',
				'is_system'			=> 0,
				'type'				=> self::TYPE_TEXT,
				'is_required'		=> false,
				'validator'			=> array(array('Alnum')),
				'filter'			=> array('StringTrim'),
			);
			$arrFieldType[self::INDEX_GENDER] = array(
				'label'				=> self::FIELD_GENDER,
                'class'             => 'length_short',
				'is_system'			=> 0,
				'is_required'		=> false,
				'validator'			=> array(),
				'filter'			=> array(),
				'type'				=> self::TYPE_GENDER
			);
			$arrFieldType[self::INDEX_COUNTRY] = array(
				'label'				=> self::FIELD_COUNTRY,
                'class'             => 'length_medium',
				'is_system'			=> 0,
				'is_required'		=> false,
				'validator'			=> array(),
				'filter'			=> array(),
				'type'				=> self::TYPE_COUNTRY
			);
            $arrFieldType[self::INDEX_CUSTOM] = array(
				'label'				=> self::FIELD_CUSTOM,
                'class'             => 'length_longest',
				'is_system'			=> 0,
				'is_required'		=> false,
				'type'				=> self::TYPE_TEXT,
                'validator'			=> array(
					array('StringLength',array('max'=>255))
				),
				'filter'			=> array('StringTrim')
			);
            
//			$arrFieldType[self::INDEX_EMAIL_NOTIFICATION] = array(
//				'label'				=> 'FIELD_EMAIL_NOTIFICATION',
//				'is_system'			=> 0,
//				'is_required'		=> true,
//				'type'				=> self::TYPE_YES_NO
//			);
		}
		return $arrFieldType;
	}


}

